package pers.liu.yibandemo.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import pers.liu.yibandemo.entity.Notification;
import pers.liu.yibandemo.entity.SchoolDepartment;
import pers.liu.yibandemo.service.NotificationService;

import java.util.List;

@RestController
@RequestMapping("/notifications")
public class NotificationController {

    @Autowired
    private NotificationService notificationService;

    @PostMapping("/add")
    public ResponseEntity<String> addNotification(@RequestBody Notification notification) {
        notificationService.addNotification(notification);
        return ResponseEntity.ok("Notification added successfully.");
    }

    @GetMapping("/{id}")
    public ResponseEntity<Notification> getNotificationById(@PathVariable Long id) {
        Notification notification = notificationService.getNotificationById(id);
        if (notification != null) {
            return ResponseEntity.ok(notification);
        } else {
            return ResponseEntity.notFound().build();
        }
    }

    @GetMapping("/all")
    public ResponseEntity<List<Notification>> getAllNotifications() {
        List<Notification> notifications = notificationService.getAllNotifications();
        return ResponseEntity.ok(notifications);
    }

    @DeleteMapping("/delete/{id}")
    public ResponseEntity<String> deleteNotification(@PathVariable Long id) {
        notificationService.deleteNotification(id);
        return ResponseEntity.ok("Notification deleted successfully.");
    }
    @PutMapping("/update")
    public ResponseEntity<String> updateNotification(@RequestBody Notification notification) {
        notificationService.updateNotification(notification);
        return ResponseEntity.ok("Notification updated successfully.");
    }
    @GetMapping("/page")
    public ResponseEntity<IPage<Notification>> getNotificationsByPage(
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize) {
        IPage<Notification> notifications = notificationService.getNotificationsByPage(pageNum, pageSize);
        return ResponseEntity.ok(notifications);
    }
}
